import { Component, EventMouse, Label, Node, _decorator } from 'cc';
import { DirectionInputEvent } from './Core/Event/DirectionInputEvent';
import EventManager from './Core/Event/EventManager';
import { PendulumManager } from './Manager/PendulumManager';
import { SinusoidManager } from './Manager/SinusoidManager';
import { WaveManager } from './Manager/WaveManager';
import Sinusoid from './Math/Sinusoid';

const { ccclass, property } = _decorator;

@ccclass('main')
export class main extends Component {

    @property({ type: Label })
    ALabel: Label = null;
    @property({ type: Label })
    lLabel: Label = null;
    @property({ type: Label })
    wLabel: Label = null;
    @property({ type: Label })
    tLabel: Label = null;

    sinusoid = new Sinusoid(50, 200, 20)

    onLoad() {
        EventManager.getInstance().on(DirectionInputEvent.UpInput, this.onUpInput, this);
        EventManager.getInstance().on(DirectionInputEvent.DownInput, this.onDownInput, this);
        EventManager.getInstance().on(DirectionInputEvent.LeftInput, this.onLeftInput, this);
        EventManager.getInstance().on(DirectionInputEvent.RightInput, this.onRightInput, this);

        this.node.on(Node.EventType.MOUSE_WHEEL, this.onMouseWheel, this);
    }

    onMouseWheel(event: EventMouse) {
        this.sinusoid.changeL(event.getScrollY() / 100);

    }
    onDestroy() {
        EventManager.getInstance().off(DirectionInputEvent.UpInput, this.onUpInput, this);
        EventManager.getInstance().off(DirectionInputEvent.DownInput, this.onDownInput, this);
        EventManager.getInstance().off(DirectionInputEvent.LeftInput, this.onLeftInput, this);
        EventManager.getInstance().off(DirectionInputEvent.RightInput, this.onRightInput, this);
    }

    onUpInput() {
        this.sinusoid.A += 1;
    }

    onDownInput() {
        this.sinusoid.A -= 1;
    }

    onLeftInput() {
        this.sinusoid.changeW(-1);
    }

    onRightInput() {
        this.sinusoid.changeW(1)
    }



    draw() {
        this.getComponentInChildren(WaveManager).draw(this.sinusoid);
        this.getComponentInChildren(PendulumManager).draw(this.sinusoid);
        this.getComponentInChildren(SinusoidManager).draw(this.sinusoid);
    }

    initData() {
        let { A, l, w, t, o, d } = this.sinusoid;
        this.ALabel.string = '振幅：' + A;
        this.lLabel.string = '波长：' + l;
        this.wLabel.string = '角速度：' + w;
        this.tLabel.string = '时间：' + t;
    }
    update(deltaTime: number) {
        this.initData();
        this.sinusoid.t += deltaTime;
        this.draw();
    }
}


